﻿using System.Web.Http;

namespace SIR.Web
{
	public static class WebApiConfig
	{
		public static void Register(HttpConfiguration config)
		{
			DAL.DataServer.Connect(System.Configuration.ConfigurationManager.ConnectionStrings["SIR"].ConnectionString);

			config.Routes.MapHttpRoute(
				name: "DefaultActionApi",
				routeTemplate: "api/{controller}/{action}/{id}",
				defaults: new { id = RouteParameter.Optional }
			);

			config.Routes.MapHttpRoute(
				name: "DefaultApi",
				routeTemplate: "api/{controller}/{id}",
				defaults: new { id = RouteParameter.Optional }
			);

			// Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type.
			// To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
			// For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
			//config.EnableQuerySupport();
		}
	}

}
/*
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Web.Http;
using System.Web.Http.OData.Builder;
using System.Web.Http.Routing;

namespace SIR.Web
{
	public static class WebApiConfig
	{
		public const string ApiDefault = "ApiDefaultRoute";

		public static void Register(HttpConfiguration config)
		{
			config.ConfigureRoutes();
			config.ConfigureOData();
			config.ConfigureFilters();
			config.ConfigureFormatters();
			config.EnableQuerySupport();
			//config.EnableSystemDiagnosticsTracing();
		}

		private static void ConfigureOData(this HttpConfiguration config)
		{
			var modelBuilder = new ODataConventionModelBuilder();

			//modelBuilder.EntitySet<CustomerSearchResultViewModel>("customersearch");

			//modelBuilder.EntitySet<ProductSearchResultViewModel>("productsearch");

			//modelBuilder.EntitySet<QuoteSearchResultViewModel>("quotesearch");

			//modelBuilder.EntitySet<CustomerSearchPartyResultViewModel>("customerpartysearch");

			//modelBuilder.EntitySet<ProductViewCategoryViewModel>("productviewcategory");

			//modelBuilder.EntitySet<RecentOpportunityByUserViewModel>("recentopportunitiessearch");

			config.Routes.MapODataRoute(
				routeName: "OData",
				routePrefix: "data",
				model: modelBuilder.GetEdmModel()
			);
		}

		private static void ConfigureRoutes(this HttpConfiguration config)
		{
			config.Routes.MapHttpRoute(
				name: ApiDefault,
				routeTemplate: "api/{controller}/{id}/{action}",
				defaults: new { action = RouteParameter.Optional },
				constraints: new { id = @"^\d+$" } // id must be all digits
			);

			config.ConfigureApiConventionRoutes();
		}

		/// <remarks>
		/// this is necessary because the ApiControllerActionSelector is not strict enough
		/// </remarks>
		private static void ConfigureApiConventionRoutes(this HttpConfiguration config)
		{
			var found = typeof(HttpMethod)
			   .GetProperties(BindingFlags.Public | BindingFlags.Static)
			   .Where(p => p.PropertyType == typeof(HttpMethod))
			   .Select(p => (HttpMethod)p.GetValue(null));

			foreach (var m in found)
			{
				config.Routes.MapHttpRoute(
					name: string.Format("Api{0}Route", m),
					routeTemplate: "api/{controller}",
					defaults: new { action = m.ToString() },
					constraints: new { httpMethod = new HttpMethodConstraint(m) }
				);
			}
		}

		private static void ConfigureFilters(this HttpConfiguration config)
		{
			//config.Filters.Add(new ApiValidateAttribute());
		}

		private static void ConfigureFormatters(this HttpConfiguration config)
		{
			config.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
			config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
		}
	}
}
*/